package com.lxl.xm.controller;

import com.lxl.xm.common.BaseResponse;
import com.lxl.xm.common.ErrorCode;
import com.lxl.xm.common.ResultUtils;
import com.lxl.xm.exception.BusinessException;
import com.lxl.xm.service.StudentTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("studentTask")
@Slf4j
public class StudentTaskController {

    @Resource
    StudentTaskService studentTaskService;

    /**
     * 获取任务的完成数
     * @param taskId
     * @return
     */
    @GetMapping("count/task")
    public BaseResponse<Long> countCompletedStudentsByTaskId(@RequestParam Long taskId){
        if (taskId == null || taskId < 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"任务ID错误");
        }
        return ResultUtils.success(studentTaskService.countCompletedStudentsByTaskId(taskId));
    }

    /**
     * 学生完成主线任务
     * @param studentId
     * @param taskId
     * @return
     */
    @PostMapping("/complete")
    public BaseResponse<Boolean> studentCompleteTask(Long studentId,Long taskId){
        if (taskId == null || studentId == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"学生或任务id错误");
        }
        return ResultUtils.success(studentTaskService.studentCompleteTask(studentId,taskId));
    }
}
